package top.ldkxingzhe.ldksettings.adapters

import android.content.pm.ApplicationInfo
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import top.ldkxingzhe.ldksettings.R
import top.ldkxingzhe.ldksettings.entities.InstalledInfoHolder

/**
 * Created by liangdengke on 2018/10/22.
 */
class InstalledAppsAdapter(private var packages: List<InstalledInfoHolder>)
    : BaseAdapter<InstalledViewHolder, InstalledInfoHolder>(packages) {

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): InstalledViewHolder {
        val rootView = LayoutInflater.from(p0.context).inflate(R.layout.list_item_install_apps, p0, false)
        return InstalledViewHolder(rootView)
    }

    override fun onBindViewHolder(p0: InstalledViewHolder, p1: Int) {
        val item = getItem(p1)
        val pm = p0.icon.context.packageManager
        p0.icon.setImageDrawable(item.applicationInfo.loadIcon(pm))
        p0.name.text = item.applicationInfo.loadLabel(pm)
        p0.packageName.text = item.applicationInfo.packageName
        p0.content.text = ""
        p0.rootView.setOnClickListener {
            onItemClickListener?.invoke(p1, item)
        }
    }
}

class InstalledViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val rootView = itemView
    val icon = itemView.findViewById<ImageView>(R.id.iv_icon)
    val name = itemView.findViewById<TextView>(R.id.tv_name)
    val packageName = itemView.findViewById<TextView>(R.id.tv_package_name)
    val content = itemView.findViewById<TextView>(R.id.tv_content)
}